package com.csun.dsms.service.impl;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.poi.excel.ExcelReader;
import cn.hutool.poi.excel.ExcelUtil;
import com.csun.dsms.comment.core.support.BaseService;
import com.csun.dsms.comment.core.utils.wrapper.WrapMapper;
import com.csun.dsms.comment.core.utils.wrapper.Wrapper;
import com.csun.dsms.mapper.HgFactoryIndustrialDataMapper;
import com.csun.dsms.model.huang.HgFactoryData;
import com.csun.dsms.model.huang.HgFactoryIndustrialData;
import com.csun.dsms.model.huang.HgQueryDto;
import com.csun.dsms.service.HgFactoryIndustrialService;
import com.github.pagehelper.PageHelper;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class HgFactoryIndustrialDataServiceImpl extends BaseService<HgFactoryIndustrialData>
        implements HgFactoryIndustrialService {

    @Resource
    private HgFactoryIndustrialDataMapper hgFactoryIndustrialDataMapper;
    
    @Override
    public List<HgFactoryIndustrialData> queryDataList(HgQueryDto hgQueryDto) {
        PageHelper.startPage(hgQueryDto.getPageNum(),hgQueryDto.getPageSize());
        return hgFactoryIndustrialDataMapper.queryListWithPage(hgQueryDto);
    }

    @Override
    public Wrapper importFactoryIndustrialData(MultipartFile file) {
        try {
            ExcelReader reader = ExcelUtil.getReader(file.getInputStream());
            List<Map<String, Object>> ExcelList = reader.readAll();
            if (CollectionUtil.isEmpty(ExcelList)) {
                return WrapMapper.wrap(Wrapper.ERROR_CODE, "Excel不能为空！");
            }
            for (Map<String, Object> excelMap : ExcelList) {
                HgFactoryIndustrialData hgFactoryIndustrialData = new HgFactoryIndustrialData();
                hgFactoryIndustrialData.setStatisticalYear(Convert.toInt(excelMap.get("统计年份")));
                hgFactoryIndustrialData.setStatisticalMonth(Convert.toInt(excelMap.get("统计月")));
                hgFactoryIndustrialData.setArea(Convert.toStr(excelMap.get("数据的区域")));
                hgFactoryIndustrialData.setAreaLevel(Convert.toInt(excelMap.get("是市的还是区的")));
                hgFactoryIndustrialData.setIndustrial(Convert.toStr(excelMap.get("产业类型")));
                hgFactoryIndustrialData.setGrowthNum(Convert.toInt(excelMap.get("增长点个数")));
                hgFactoryIndustrialData.setGrowthRate(Convert.toDouble(excelMap.get("增速")));
                hgFactoryIndustrialData.setTopRate(Convert.toDouble(excelMap.get("上一级增速")));
                hgFactoryIndustrialData.setRanking(Convert.toInt(excelMap.get("排名")));
                hgFactoryIndustrialData.setExpectTotal(Convert.toInt(excelMap.get("预增产值")));
                hgFactoryIndustrialData.setYearTotal(Convert.toDouble(excelMap.get("年产值")));
                hgFactoryIndustrialData.setPullRate(Convert.toDouble(excelMap.get("拉动产业增长率")));
                hgFactoryIndustrialData.setEntryDepartment(Convert.toStr(excelMap.get("上传的部门")));
                hgFactoryIndustrialData.setCreator(Convert.toStr(excelMap.get("上传人")));
                hgFactoryIndustrialData.setApproved(Convert.toInt(excelMap.get("是否审核")));
                hgFactoryIndustrialData.setApproval(Convert.toStr(excelMap.get("审核人")));
                hgFactoryIndustrialData.setCreatedTime(new Date());
                hgFactoryIndustrialDataMapper.insert(hgFactoryIndustrialData);
            }
            return WrapMapper.wrap(Wrapper.SUCCESS_CODE, Wrapper.SUCCESS_MESSAGE, "导入工业的主导产业数据成功！");
        } catch (Exception e) {
            return WrapMapper.wrap(Wrapper.ERROR_CODE, e.getMessage());
        }
    }


}
